package com.global.college.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.global.college.constants.Constant;
import com.global.college.utils.PrefUtils;

import butterknife.ButterKnife;

public abstract class BaseImmersiveFragment extends ImmersionFragment {

    protected String TAG = getClass().getSimpleName();
    public Context context;
    protected View mRootView;


    @Override
    public void onAttach(Context context) {
        this.context = context;
        super.onAttach(context);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(getContentViewId(), container, false);
        ButterKnife.bind(this, mRootView);
        initView(inflater, container, savedInstanceState);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initEvent();
    }

    /**
     * this method must be implement by child activity, instead of setContentView in child activity
     */
    public abstract int getContentViewId();

    /**
     * 子类实现此抽象方法返回view进行展示
     *
     * @param inflater
     * @return
     */
    public abstract void initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    protected abstract void initData();

    /**
     * 事件处理
     */
    protected void initEvent() {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 判断是否登陆进行跳转
     * <p>
     * 根据传入的类(class)打开指定的activity
     *
     * @param pClass 登录状态要进入的界面
     */
    public void JugeAndOpenActivity(Class<?> pClass, Bundle bundle) {
        String token = PrefUtils.getString(Constant.User.TOKEN, "");
        if (!TextUtils.isEmpty(token)) {
            Intent itent = new Intent();
            itent.putExtras(bundle);
            itent.setClass(context, pClass);
            startActivity(itent);
        } else {
            // ToastUtils.showToastShort("请您先登录！");
//            startActivity(new Intent(context, LoginActivity.class));
        }

    }

    /**
     * 根据传入的类(class)打开指定的activity
     *
     * @param pClass
     */
    public void JugeAndOpenActivity(Class<?> pClass) {
        String token = PrefUtils.getString(Constant.User.TOKEN, "");
        if (!TextUtils.isEmpty(token)) {
            Intent itent = new Intent();
            itent.setClass(context, pClass);
            startActivity(itent);
        } else {
            //ToastUtils.showToastShort("请您先登录！");
//            startActivity(new Intent(context, LoginActivity.class));
        }
    }

}

